﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Office.Server.Search.Administration;
using Microsoft.Office.Server.Search.Cmdlet;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.PowerShell;

namespace Community.SharePoint.PowerShell
{
    public class ScopeDisplayGroupPipeBind : SPCmdletPipeBind<ScopeDisplayGroup>
    {
        // Fields
        private int id;
        private ScopeDisplayGroup pipedObject;

        // Methods
        public ScopeDisplayGroupPipeBind(ScopeDisplayGroup inputObject)
            : base(inputObject)
        {
        }

        public ScopeDisplayGroupPipeBind(string stringValue)
        {
            this.id = Convert.ToInt32(stringValue);
        }

        protected override void Discover(ScopeDisplayGroup scopeDisplayGroup)
        {
            this.id = scopeDisplayGroup.ID;
            this.pipedObject = scopeDisplayGroup;
        }

        public override ScopeDisplayGroup Read()
        {
            return this.pipedObject;
        }

        public ScopeDisplayGroup Read(SearchServiceApplicationPipeBind searchAppPipeBind, Uri siteUrl, string name)
        {
            if (this.pipedObject != null)
            {
                return this.pipedObject;
            }

            SearchServiceApplication searchServiceApplication = null;
            if (searchAppPipeBind != null)
            {
                searchServiceApplication = searchAppPipeBind.Read();
            }

            if (searchServiceApplication == null)
            {
                throw new InvalidOperationException("Search Application Not Specified");
            }

            Scopes scopes = new Scopes(searchServiceApplication);
            return scopes.GetDisplayGroup(siteUrl, name);
        }

    }
}
